package com.hyl.icon.testdemo;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class WaterBrandLogoAdaptive {

    public static void main(String[] args) {
        String backgroundPath = "D:\\workspace\\icon\\test\\images\\background.jpg";  // 背景图片路径
        String logoPath = "D:\\workspace\\icon\\test\\images\\logo.png";             // Logo 图片路径
        String outputPath = "D:\\workspace\\icon\\test\\images\\output_logo.png";    // 输出图片路径
        String brandName = "菠萝甜心橙";  // 竖排品牌名称
        String fontPath = "D:\\workspace\\icon\\test\\font\\SourceHanSansSC-VF.ttf"; // 本地字体文件路径

        try {
            // 读取背景图片
            BufferedImage background = ImageIO.read(new File(backgroundPath));
            int width = background.getWidth();
            int height = background.getHeight();

            // 创建新图像
            BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = outputImage.createGraphics();

            // 开启抗锯齿
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // 画背景图
            g2d.drawImage(background, 0, 0, null);

            // 计算 Logo 尺寸（占背景高度的 1/4）
            int logoHeight = height / 5;
            int logoWidth = logoHeight;  // 这里假设 Logo 是正方形

            // 计算 Logo 位置（靠近顶部）
            int logoX = (width - logoWidth) / 2;
            int logoY = height / 40;  // 调整后，Logo 离顶部更近

            // 读取 Logo 图片并缩放
            BufferedImage logo = ImageIO.read(new File(logoPath));
            BufferedImage resizedLogo = resizeImage(logo, logoWidth, logoHeight);
            g2d.drawImage(resizedLogo, logoX, logoY, null);

            // 计算品牌名称区域高度（占背景高度的 1/2）
            int brandHeight = height / 3;
            int numChars = brandName.length();
            int fontSize = brandHeight / numChars;  // 计算字体大小，使所有字整体占 1/2 高度

            // 限制最小字体大小，防止过小
            fontSize = Math.max(fontSize, height / 20);

            // 加载自定义字体
            Font customFont = loadCustomFont(fontPath, fontSize);
            g2d.setFont(customFont);

            // 设置品牌名颜色
            g2d.setColor(Color.BLACK);

            // **增加 Logo 和品牌名称之间的间距**
            int spaceBetween = logoHeight / 3; // **增大间距，防止接触**

            // 计算品牌名的起始位置（竖排居中）
            int textStartY = logoY + logoHeight + spaceBetween;
            int textX = (width - fontSize) / 2;

            // 绘制品牌名称（竖排）
            for (char c : brandName.toCharArray()) {
                g2d.drawString(String.valueOf(c), textX, textStartY);
                textStartY += fontSize;  // 字体大小决定行间距
            }

            // 释放资源
            g2d.dispose();

            // 保存图片
            ImageIO.write(outputImage, "PNG", new File(outputPath));
            System.out.println("竖排 Logo 图片已保存到 " + outputPath);

        } catch (IOException | FontFormatException e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载本地 TTF 字体
     */
    private static Font loadCustomFont(String fontPath, int fontSize) throws IOException, FontFormatException {
        File fontFile = new File(fontPath);
        Font customFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
        return customFont.deriveFont(Font.BOLD, fontSize);
    }

    /**
     * 缩放图片
     */
    private static BufferedImage resizeImage(BufferedImage originalImage, int width, int height) {
        BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = resizedImage.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2d.drawImage(originalImage, 0, 0, width, height, null);
        g2d.dispose();
        return resizedImage;
    }
}
